package com.zk.monitormanagement.activity;

import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;
import com.videogo.widget.CheckTextButton;
import com.zk.monitormanagement.R;
import com.zk.monitormanagement.activity.realplay.EZRealPlayActivity;
import com.zk.monitormanagement.app.MyApplication;
import com.zk.monitormanagement.base.BaseActivityImp;
import com.zk.monitormanagement.bean.EzTokenBean;
import com.zk.monitormanagement.bean.MonitorBean;
import com.zk.monitormanagement.constant.ManageConstants;
import com.zk.monitormanagement.utils.EZUtils;
import com.zk.monitormanagement.utils.SharedPreferUtils;
import com.zk.monitormanagement.utils.ToastUtil;
import com.zk.monitormanagement.view.LoadingTextView;

import butterknife.BindView;

public class MainActivity extends BaseActivityImp {


    @BindView(R.id.realplay_sv)
    SurfaceView realplaySv;
    @BindView(R.id.realplay_tip_tv)
    TextView realplayTipTv;
    @BindView(R.id.realplay_play_iv)
    ImageView realplayPlayIv;
    @BindView(R.id.realplay_loading)
    LoadingTextView realplayLoading;
    @BindView(R.id.realplay_privacy_tv)
    TextView realplayPrivacyTv;
    @BindView(R.id.realplay_privacy_ly)
    LinearLayout realplayPrivacyLy;
    @BindView(R.id.realplay_page_anim_iv)
    ImageView realplayPageAnimIv;
    @BindView(R.id.realplay_loading_rl)
    RelativeLayout realplayLoadingRl;
    @BindView(R.id.vg_play_window)
    RelativeLayout vgPlayWindow;
    @BindView(R.id.realplay_play_btn)
    ImageButton realplayPlayBtn;
    @BindView(R.id.sound_line)
    View soundLine;
    @BindView(R.id.realplay_sound_btn)
    ImageButton realplaySoundBtn;
    @BindView(R.id.quality_line)
    View qualityLine;
    @BindView(R.id.realplay_quality_btn)
    Button realplayQualityBtn;
    @BindView(R.id.flow_line)
    View flowLine;
    @BindView(R.id.realplay_flow_tv)
    TextView realplayFlowTv;
    @BindView(R.id.mirror_line)
    View mirrorLine;
    @BindView(R.id.fullscreen_button)
    CheckTextButton fullscreenButton;
    @BindView(R.id.realplay_control_rl)
    LinearLayout realplayControlRl;
    @BindView(R.id.realplay_play_rl)
    RelativeLayout realplayPlayRl;
    @BindView(R.id.realplay_ptz_btn)
    ImageButton realplayPtzBtn;
    @BindView(R.id.realplay_ptz_btn_ly)
    LinearLayout realplayPtzBtnLy;
    @BindView(R.id.realplay_talk_btn)
    ImageButton realplayTalkBtn;
    @BindView(R.id.realplay_talk_btn_ly)
    LinearLayout realplayTalkBtnLy;
    @BindView(R.id.realplay_ssl_btn)
    Button realplaySslBtn;
    @BindView(R.id.realplay_ssl_progress)
    ProgressBar realplaySslProgress;
    @BindView(R.id.realplay_ssl_btn_rl)
    RelativeLayout realplaySslBtnRl;
    @BindView(R.id.realplay_ssl_tv)
    TextView realplaySslTv;
    @BindView(R.id.realplay_ssl_btn_ly)
    LinearLayout realplaySslBtnLy;
    @BindView(R.id.realplay_privacy_btn)
    ImageButton realplayPrivacyBtn;
    @BindView(R.id.realplay_privacy_btn_ly)
    LinearLayout realplayPrivacyBtnLy;
    @BindView(R.id.realplay_previously_btn)
    ImageButton realplayPreviouslyBtn;
    @BindView(R.id.realplay_previously_btn_ly)
    LinearLayout realplayPreviouslyBtnLy;
    @BindView(R.id.realplay_video_btn)
    ImageButton realplayVideoBtn;
    @BindView(R.id.realplay_video_start_btn)
    ImageButton realplayVideoStartBtn;
    @BindView(R.id.realplay_video_container)
    FrameLayout realplayVideoContainer;
    @BindView(R.id.realplay_video_container_ly)
    LinearLayout realplayVideoContainerLy;
    @BindView(R.id.realplay_operate_ly)
    LinearLayout realplayOperateLy;
    @BindView(R.id.ezopen_realplay_operate_bar)
    HorizontalScrollView ezopenRealplayOperateBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        getEzToken();
    }
    private void getEzToken() {
        OkGo.<String>get(ManageConstants.getYsToken)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        EzTokenBean ezTokenBean = gson.fromJson(response.body(),EzTokenBean.class);
                        if (ezTokenBean.getStatus().equals("1")){
                            MyApplication.getOpenSDK().setAccessToken(ezTokenBean.getToken());
                            getRealPlayData();
                        }else {
                            ToastUtil.show(ezTokenBean.getMsg());
                        }
                    }
                });
    }

    private void getRealPlayData() {
        OkGo.<String>get(ManageConstants.getMonitor)
                .params("unitid", SharedPreferUtils.getInstance().get(MainActivity.this,"unitid"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson=new Gson();
                        final MonitorBean monitorBean=gson.fromJson(response.body(),MonitorBean.class);
//                        EZPlayer player  = MyApplication.getOpenSDK().createPlayer(monitorBean.getData().get(11).getCameras().get(0).getDeviceserial(),0);
                        EZPlayer player  = MyApplication.getOpenSDK().createPlayer("742350312",0);

                        //设置Handler, 该handler将被用于从播放器向handler传递消息
//                        player.setHandler(mHandler);

                        //设置播放器的显示Surface  realplay_sv
                        player.setSurfaceHold(realplaySv.getHolder());

                        /**
                         * 设备加密的需要传入密码
                         * 传入视频加密密码，用于加密视频的解码，该接口可以在收到ERROR_INNER_VERIFYCODE_NEED或ERROR_INNER_VERIFYCODE_ERROR错误回调时调用
                         * @param verifyCode 视频加密密码，默认为设备的6位验证码
                         */
//                        player.setPlayVerifyCode(String verifyCode);

                        //开启直播
                        player.startRealPlay();
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                try {
//                                    Log.e("sss",monitorBean.getData().get(11).getCameras().get(0).getDeviceserial());
//                                    mDeviceInfo = MyApplication.getOpenSDK().getDeviceInfo(monitorBean.getData().get(11).getCameras().get(0).getDeviceserial());
//                                    mCameraInfo=  EZUtils.getCameraInfoFromDevice(mDeviceInfo, 0);
//                                    runOnUiThread(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            Log.e("sss","sss-->"+mCameraInfo.getCameraName());
//                                            startRealPlay();
//                                        }
//                                    });
//                                } catch (BaseException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }).start();

//                        mCameraInfo =
//                        mDeviceInfo = intent.getParcelableExtra(IntentConsts.EXTRA_DEVICE_INFO);
                    }
                });
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }


}
